package com.rtp2p.rtcoresdkdemo.replay

import android.os.Bundle
import androidx.activity.viewModels
import androidx.databinding.DataBindingUtil
import com.rtp2p.rtcoresdkdemo.R
import com.rtp2p.rtcoresdkdemo.base.BaseActivity
import com.rtp2p.rtcoresdkdemo.databinding.ReplayActivityBinding

class ReplayActivity : BaseActivity() {
    private val viewModel: ReplayViewModel by viewModels()
    private var adapter: ReplayAdapter? = null
    override fun initWorkspaceAction(savedInstanceState: Bundle?) {
        val binding = DataBindingUtil.setContentView<ReplayActivityBinding>(
            this,
            R.layout.replay_activity
        )
        setContentView(binding.root)
        setOnApplyWindowInsetsListener(binding.root)

        val deviceId = intent.getStringExtra("deviceId")
        if (deviceId.isNullOrEmpty()) {
            finish()
            return
        }
        val isAp = intent.getBooleanExtra("isAp",false)

        adapter = ReplayAdapter {
            viewModel.play(it)
        }


        viewModel.init(this,deviceId,isAp, binding.rtVideoView) {
            adapter?.onRefreshData(it)
            binding.swipeRefreshLayout.finishLoadMore()
        }

        binding.lifecycleOwner = this
        binding.adapter = adapter
        binding.vm = viewModel

        binding.btnReturn.setOnClickListener { finish() }
        //禁止下拉刷新
        binding.swipeRefreshLayout.setEnableRefresh(false)
        // 设置上拉加载监听器
        binding.swipeRefreshLayout.setOnLoadMoreListener {
            viewModel.loadRecords()
            it.finishLoadMore(3_000);// 加载完成后等待的时间
        }
    }

    override fun onStart() {
        super.onStart()
        viewModel.onStart()
    }

    override fun onStop() {
        super.onStop()
        viewModel.onStop()
    }
}